import 'package:yaml/yaml.dart' as yaml;

import '../utils/file_utils.dart';
import 'config_exception.dart';

class PubspecConfig {
  String? _projectName;
  // bool? _enabled;
  // String? _className;
  // String? _mainLocale;
  // String? _arbDir;
  // String? _outputDir;
  // bool? _useDeferredLoading;

  PubspecConfig() {
    var pubspecFile = getPubspecFile();
    if (pubspecFile == null) {
      throw ConfigException("Can't find 'pubspec.yaml' file.");
    }

    var pubspecFileContent = pubspecFile.readAsStringSync();
    var pubspecYaml = yaml.loadYaml(pubspecFileContent);

    if (pubspecYaml is! yaml.YamlMap) {
      throw ConfigException(
          "Failed to extract config from the 'pubspec.yaml' file.\nExpected YAML map but got ${pubspecYaml.runtimeType}.");
    }

    _projectName = pubspecYaml['name'] is String
        ? pubspecYaml['name']
        : null;
    if (_projectName == null) {
      throw ConfigException("Can't find 'name' field from 'pubspec.yaml' file.");
    }

    // var flutterIntlConfig = pubspecYaml['flutter_intl'];
    // if (flutterIntlConfig == null) {
    //   return;
    // }
    //
    // _enabled = flutterIntlConfig['enabled'] is bool
    //     ? flutterIntlConfig['enabled']
    //     : null;
    // _className = flutterIntlConfig['class_name'] is String
    //     ? flutterIntlConfig['class_name']
    //     : null;
    // _mainLocale = flutterIntlConfig['main_locale'] is String
    //     ? flutterIntlConfig['main_locale']
    //     : null;
    // _arbDir = flutterIntlConfig['arb_dir'] is String
    //     ? flutterIntlConfig['arb_dir']
    //     : null;
    // _outputDir = flutterIntlConfig['output_dir'] is String
    //     ? flutterIntlConfig['output_dir']
    //     : null;
    // _useDeferredLoading = flutterIntlConfig['use_deferred_loading'] is bool
    //     ? flutterIntlConfig['use_deferred_loading']
    //     : null;
  }

  String get projectName => _projectName!;
  // bool? get enabled => _enabled;
  //
  // String? get className => _className;
  //
  // String? get mainLocale => _mainLocale;
  //
  // String? get arbDir => _arbDir;
  //
  // String? get outputDir => _outputDir;
  //
  // bool? get useDeferredLoading => _useDeferredLoading;
}